package model

import (
	"../global"
	"github.com/go-pg/pg"
)

type BrokerageModel struct{}

//生成新的记录
func (*BrokerageModel) CreateBrokerage(query *Brokerage) (err error) {
	_, err = global.DB.Model(query).Insert()
	return
}

//获取列表
func (*BrokerageModel) GetBrokerage(query *Brokerage, column []string, listParams *ListParams) (result []Brokerage, err error) {
	model := global.DB.Model(&result)
	for k := range column {
		model.Column(column[k])
	}
	if query.ID != 0 {
		model.Where("id=?", query.ID)
	}
	if query.UserID != 0 {
		model.Where("user_id=?", query.UserID)
	}

	if query.Status == true {
		model.Where("status=?", true)
	}
	if query.Amount != 0 {
		model.Where("amount=?", query.Amount)
	}
	if query.Time != 0 {
		model.Where("time=?", query.Time)
	}

	model.Limit(listParams.Limit).Order("time DESC")
	if listParams.Limit != 0 && listParams.Page != 0 {
		err = model.Offset((listParams.Page - 1) * listParams.Limit).Select()
	} else {
		err = model.Select()
	}
	return
}

//删除结算记录
func (*BrokerageModel) DelBrokerage(IDs []int64) (err error) {
	if len(IDs) > 1 {
		_, err = global.DB.Model(&Brokerage{}).WhereIn("id in (?)", pg.In(IDs)).Delete()
	} else {
		_, err = global.DB.Model(&Brokerage{}).Where("id=?", IDs[0]).Delete()
	}
	return
}
